<?php

namespace app\app\controller;

use app\app\model\ProjectModel;
use app\BaseController;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;

class JobTicket extends BaseController
{

    /**
     * 作业票统计数据
     */
    public function analysis()
    {
        $ticket_total = Db::table('t_ticket_stat')->count();
        $ticket_executing = Db::table('t_ticket_stat')->where('process_status', '执行中')->count();
        $ticket_stop = Db::table('t_ticket_stat')->where('process_status', '暂停中')->count();
        $ticket_finsh = Db::table('t_ticket_stat')->where('process_status', '已完成')->count();
        $res = [
            'ticket_total' => $ticket_total,
            'ticket_executing' => $ticket_executing,
            'ticket_stop' => $ticket_stop,
            'ticket_finsh' => $ticket_finsh,
        ];
        $this->success('success', $res);
    }


    /**
     * 获取作业票项目列表
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function listGroup()
    {
        $name = input('name/s', '');
        $process_status = input('process_status/s', '全部');
//        $page_size = input('pageSize', '20');
        $page_size = 999;
        // todo 代码待修改

        $start_time = date('Y-01-01 00:00:00', time());
        $end_time = date('Y-12-31 23:59:59', time());
        $where[] = ['b.tc_time', '>=', $start_time];
        $where[] = ['b.kg_time', '<=', $end_time];
        if ($name) {
            $where[] = ['b.project_name|c.project_name', 'like', '%' . $name . '%'];
        }
        if ($process_status != '全部') {
            $where[] = ['a.process_status', '=', $process_status];
        }

        $project_ticket = Db::table('t_ticket_stat')
            ->alias('a')
            ->leftJoin('t_project b', 'a.project_id = b.id')
            ->leftJoin('t_project c', 'b.link_project_id = c.id')
            ->where($where)
            ->group('b.id')
            ->field('b.id,b.link_project_id,b.is_parent')
            ->paginate($page_size);

        $project_ticket = $project_ticket->toArray();

        $page = [
            'total' => $project_ticket['total'],
            'per_page' => $project_ticket['per_page'],
            'current_page' => $project_ticket['current_page'],
            'last_page' => $project_ticket['last_page'],
        ];

        $project_ticket = $project_ticket['data'];

        $project_id_array = array_column($project_ticket, null, 'id');
        $project_id_array = array_keys($project_id_array);

        $result = [];
        foreach ($project_ticket as $v) {
            if ($v['is_parent'] == 0) {
                $result[$v['id']] = $v;
                continue;
            }

            $result[$v['link_project_id']]['children'][$v['id']]['children'][] = $v;
        }

        $project_id_array = array_merge($project_id_array, array_keys($result));

        $project = ProjectModel::where('id', 'in', $project_id_array)
            ->field('id, project_name')
            ->select();
        $project = array_column($project->toArray(), null, 'id');

        $tmp = $this->getProject($result, $project);
        $result = $page;
        $result['data'] = $tmp;

        $this->success('success', $result);
    }

    /**
     * 整理项目组列表
     * @param $project_ticket
     * @param $project
     * @return array
     */
    private function getProject($project_ticket, $project)
    {
        foreach ($project_ticket as $k => &$v) {
            $tmp = empty($v['children']) ? [] : $v['children'];
            if (isset($project[$k])) {
                $v = $project[$k];
                $v['children'] = $this->getProject($tmp ?: [], $project);

                if (isset($v['children'][0]['link_project_id'])) {
                    unset($v['children']);
                }
            }
        }

        return array_values($project_ticket);
    }

    /**
     * 作业票列表
     * @return void
     * @throws DbException
     */
    public function ticketList()
    {
        $id = input('id/s');
        if (!$id) $this->error('请传入id');
        $page_size = input('pageSize', '20');

        $process_status = input('process_status/s', '全部');
        $where[] = ['project_id', '=', $id];
        if ($process_status != '全部') {
            $where[] = ['process_status', '=', $process_status];
        }

        $meeting = Db::table('t_ticket_stat')
            ->where($where)
            ->paginate($page_size);
        if (empty($meeting)) $this->error('未找到符合条件的作业票');

        $this->success('success', $meeting);
    }

    /**
     * 作业票列表[统计页面]
     * @return void
     * @throws DbException
     */
    public function ticketListStat()
    {
        $id = input('id/s');
        $process_status = input('process_status/s', '全部');
        $where[] = ['project_id', '=', $id];
        if ($process_status != '全部') {
            $where[] = ['process_status', '=', $process_status];
        }


        if (!$id) $this->error('请传入id');
        $data = Db::table('t_ticket_stat')
            ->where($where)
            ->select();
        if (empty($data)) $this->error('未找到符合条件的作业票');

        $this->success('success', $data);
    }


    /**
     * 获取作业票详情
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function details()
    {
        $id = input('id/s');
        if (!$id) $this->error('请传入id');
        $list = Db::table('t_ticket_stat')->find($id);
        $this->success('success', $list);
    }

}
